<template>
  <div class="app-container" >
    <el-card class="box-card">
      <el-form :model="ruleForm" status-icon :rules="loginRules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="原始密码" prop="pwd">
          <el-input type="text" v-model="ruleForm.pwd" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="确认密码" prop="checkPass">
          <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click.native.prevent="submitForm" :loading="loading">提交</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>

      </el-form>
    </el-card>
  </div>
</template>

<script>

import {updatePassword,logout} from '@/api/user'

export default {
  name: "index",
  data() {
    const checkPwd = (rule, value, callback) => {
      if (!value) {
        callback(new Error('原始密码不能为空'));
      }
      callback();
    };
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('新密码不能为空'));
      }else if (value.length < 4) {
        callback(new Error('密码不能少于4位'))
      } else {
        callback();
      }
    };
    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        pwd:'',
        password: '',
        checkPass: ''
      },
      loginRules: {
        pwd: [{ validator: checkPwd, trigger: 'blur',required: true}],
        password: [{ validator: validatePass, trigger: 'blur',required: true}],
        checkPass: [{ validator: validatePass2, trigger: 'blur',required: true}]
      },
      loading:false
    };
  },
  methods: {
    submitForm() {
      this.$refs.ruleForm.validate(valid => {
        console.log(valid)
        if (valid) {
          this.loading = true
          const user = {
            'pwd':this.ruleForm.pwd,
            'password':this.ruleForm.password
          }
          // 执行修改密码操作
          updatePassword(user).then(res =>{
            if(res.data){
              this.$message({
                message: '密码修改成功,请重新登录',
                type: 'success'
              });
              setTimeout(() => {
                this.logout();
              },1000)
            }
          })
          console.log(this.ruleForm)
        } else {
          console.log('修改密码失败!!')
          return false
        }
      });
    },
    async logout() {
      await this.$store.dispatch('user/logout')
      this.$router.push(`/login`)
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>

</style>
